package com.tjsinfo.tjsoa.doc.dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.tjsinfo.tjsoa.doc.vo.TDocDocOpe;

/**
 * A data access object (DAO) providing persistence and search support for
 * TDocDocOpe entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.tjsinfo.tjsoa.doc.vo.TDocDocOpe
 * @author MyEclipse Persistence Tools
 */

public class TDocDocOpeDAO extends HibernateDaoSupport implements ITDocDocOpeDAO {
	private static final Log log = LogFactory.getLog(TDocDocOpeDAO.class);

	protected void initDao() {
		// do nothing
	}

	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.doc.dao.ITDocDocOpeDAO#save(com.tjsinfo.tjsoa.doc.vo.TDocDocOpe)
	 */
	public void save(TDocDocOpe transientInstance) {
		log.debug("saving TDocDocOpe instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.doc.dao.ITDocDocOpeDAO#delete(com.tjsinfo.tjsoa.doc.vo.TDocDocOpe)
	 */
	public void delete(TDocDocOpe persistentInstance) {
		log.debug("deleting TDocDocOpe instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.doc.dao.ITDocDocOpeDAO#findById(java.lang.String)
	 */
	public TDocDocOpe findById(java.lang.String id) {
		log.debug("getting TDocDocOpe instance with id: " + id);
		try {
			TDocDocOpe instance = (TDocDocOpe) getHibernateTemplate().get(
					"com.tjsinfo.tjsoa.doc.vo.TDocDocOpe", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.doc.dao.ITDocDocOpeDAO#findByExample(com.tjsinfo.tjsoa.doc.vo.TDocDocOpe)
	 */
	public List findByExample(TDocDocOpe instance) {
		log.debug("finding TDocDocOpe instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.doc.dao.ITDocDocOpeDAO#findByProperty(java.lang.String, java.lang.Object)
	 */
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding TDocDocOpe instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from TDocDocOpe as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.doc.dao.ITDocDocOpeDAO#findAll()
	 */
	public List findAll() {
		log.debug("finding all TDocDocOpe instances");
		try {
			String queryString = "from TDocDocOpe";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.doc.dao.ITDocDocOpeDAO#merge(com.tjsinfo.tjsoa.doc.vo.TDocDocOpe)
	 */
	public TDocDocOpe merge(TDocDocOpe detachedInstance) {
		log.debug("merging TDocDocOpe instance");
		try {
			TDocDocOpe result = (TDocDocOpe) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.doc.dao.ITDocDocOpeDAO#attachDirty(com.tjsinfo.tjsoa.doc.vo.TDocDocOpe)
	 */
	public void attachDirty(TDocDocOpe instance) {
		log.debug("attaching dirty TDocDocOpe instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.doc.dao.ITDocDocOpeDAO#attachClean(com.tjsinfo.tjsoa.doc.vo.TDocDocOpe)
	 */
	public void attachClean(TDocDocOpe instance) {
		log.debug("attaching clean TDocDocOpe instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void deleteA(String docId) {
		log.debug("deleting TDocDocOpe instance");
		try {
			List<TDocDocOpe> list = getHibernateTemplate().find("from TDocDocOpe where docId = '"+docId+"'");
			for(TDocDocOpe tddo : list){
				getHibernateTemplate().delete(tddo);
			}
			log.debug("deleting successful");
		} catch (RuntimeException re) {
			log.error("deleting failed", re);
			throw re;
		}
	}		
	
	public static ITDocDocOpeDAO getFromApplicationContext(ApplicationContext ctx) {
		return (ITDocDocOpeDAO) ctx.getBean("TDocDocOpeDAO");
	}
}